/*
 * MonomialProcedure.java
 *
 * Created on May 4, 2005, 7:27 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.milowski.monos.scheme;

import java.util.*;

import gnu.mapping.*;
import gnu.lists.*;
import com.milowski.monos.*;
import com.milowski.monos.monomial.*;

/**
 *
 * @author R. Alexander Milowski
 */
public class LexOrderedListProcedure extends ProcedureN
{
   
   /** Creates a new instance of MonomialProcedure */
   public LexOrderedListProcedure()
   {
      super("lexed-list-with-order");
   }
   
   public Object applyN(Object [] args) {
      if (args.length<1) {
         throw new RuntimeException(name()+" expects at least two arguments.");
      }
      /*
      if (args.length==1 && args[0] instanceof LList) {
         LList argList = (LList)args[0];
         if (LList.length(argList)==0) {
            throw new RuntimeException(name()+" in this form expects a list of length at least two whose first argument is the lex order.");
         }
         LexOrder order = (LexOrder)argList.get(0);
         LexOrderedList l = new LexOrderedList(order);
         Iterator exps = argList.iterator();
         exps.next();
         while (exps.hasNext()) {
            try {
               Object o = exps.next();
               if (o instanceof String) {
                  l.add(Monomial.toMonomial(order, (String)o));
               } else {
                  l.add(o);
               }
            } catch (FormatException ex) {
               throw new RuntimeException(ex.getMessage());
            }
         }
         return l;
      } else {
         LexOrder order = (LexOrder)args[0];
         if (args.length==2 && args[1] instanceof LList) {

            LList list = (LList)args[1];
            LexOrderedList l = new LexOrderedList(order);

            for (Iterator exps = list.iterator(); exps.hasNext(); ) {
               try {
                  Object o = exps.next();
                  if (o instanceof String) {
                     l.add(Monomial.toMonomial(order, (String)o));
                  } else {
                     l.add(o);
                  }
               } catch (FormatException ex) {
                  throw new RuntimeException(ex.getMessage());
               }
            }
            return l;

         } else {
       */
      LexOrder order = (LexOrder)args[0];
      LexOrderedList l = new LexOrderedList(order);
      for (int i=1; i<args.length; i++) {
         l.add(args[i]);
      }
      return l;
   }
   
}
